﻿using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

namespace XunPan.Web.Globals
{

    //*********************************************************************
    //
    // ImageUtility 类
    //
    // 包含获取和处理图片数据的静态方法
    //
    //*********************************************************************
    /// <summary> 
    /// 包含获取和处理图片数据的静态方法
    /// </summary> 

    public class ImageUtility
    {




        //*********************************************************************
        //
        // IsImageFile 方法
        //
        // Returns true or false depending on whether the filename 
        // ends with an image file extension. 
        //
        //*********************************************************************

        /// <summary>
        /// Returns true or false depending on whether the filename ends with an image file extension. 
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public static bool IsImageFile(string fileName)
        {
            fileName = fileName.ToLower();
            if (fileName.EndsWith(".gif") || fileName.EndsWith(".jpg") || fileName.EndsWith(".jpeg") || fileName.EndsWith(".bmp"))
            {
                return true;
            }
            return false;
        }
        //IsImageFile



        //*********************************************************************
        //
        // ModifyImage 方法
        //
        // Modifies the width or height of an image. 
        //
        //*********************************************************************
        /// <summary>
        /// Modifies the width or height of an image. 
        /// </summary>
        /// <param name="original">byte array of the image原始图片byte数组</param>
        /// <param name="width">new image width 目标宽度，设为-1则表示以高度为缩放基准</param>
        /// <param name="height">new image height 目标高度，设为-1则表示以宽度为缩放基准</param>
        /// <param name="contentType">type of image (this should be either pjpeg or gif) 图片类型</param>
        /// <param name="isThumbNail">compression or not 是否为缩略图</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public static byte[] ModifyImage(byte[] original, int width, int height, string contentType, bool isThumbNail)
        {

            // original:	byte array of the image 
            // width:		new image width
            // height:		new image height
            // contentType:	type of image (this should be either pjpeg or gif)
            // isThumbnail:	
            // convert byte array to image
            MemoryStream stream = new MemoryStream(original);

            // 
            // convert size to new dimensions
            Bitmap bmp = (Bitmap)Image.FromStream(stream);

            // Calculate missing width or height
            if (width == -1)
            {
                width = Convert.ToInt32(System.Convert.ToDouble(height) / System.Convert.ToDouble(bmp.Height) * System.Convert.ToDouble(bmp.Width));
            }
            if (height == -1)
            {
                height = Convert.ToInt32(System.Convert.ToDouble(width) / System.Convert.ToDouble(bmp.Width) * System.Convert.ToDouble(bmp.Height));
            }
            Bitmap thumbBmp = new Bitmap(bmp, width, height);
            //thumbBmp.Palette = GetTransparentColorPalette(bmp)

            // convert to stream in preparation to convert to byte array
            stream = new MemoryStream();

            // save the stream 
            // first we need to determine if image is JPG or GIF or BMP
            // to determine the proper encoder type
            //IE will refer JPG JPEG to PJPEG
            //Firefox will refer them to JPEG
            if (contentType.ToLower().EndsWith("jpeg"))
            {
                // is this a thumbnail?
                if (isThumbNail)
                {
                    stream = SaveJpgWithCompression(thumbBmp, 40);


                }
                else
                {
                    // thumbBmp.Save ("C:\\atest\\savingtest.jpg",ImageFormat.Jpeg);
                    thumbBmp.Save(stream, ImageFormat.Jpeg);
                }

            }
            else if (contentType.ToLower().EndsWith("bmp"))
            {
                // is this a thumbnail?

                if (isThumbNail)
                {
                    thumbBmp.Save(stream, ImageFormat.Jpeg);

                    //or will compress bmp file
                    //stream = SaveBmpWithCompression(thumbBmp, 40)

                }
                else
                {
                    // thumbBmp.Save ("C:\\atest\\savingtest.jpg",ImageFormat.Jpeg);
                    thumbBmp.Save(stream, ImageFormat.Bmp);
                }


            }
            else
            {
                //thumbBmp.Save ("C:\\atest\\savingtest.gif",ImageFormat.Gif);
                thumbBmp.Save(stream, ImageFormat.Gif);
            }
            //tranparency is not supported in IE for png files

            // cleanup
            thumbBmp.Dispose();
            bmp.Dispose();

            return stream.GetBuffer();
        }
        //ModifyImage




        //*********************************************************************
        //
        // SaveJpgWithCompression 方法
        //
        // Compresses a JPEG image. 
        //
        //*********************************************************************

        /// <summary>
        /// Compresses a JPEG image with a new name
        /// </summary>
        /// <param name="original">The image to compress</param>
        /// <param name="fileName">	The filename to save the JPG to.</param>
        /// <param name="compression">A value from 1 to 100 A value of 100 is no compression. 
        /// A value of 1 is maximum compression.
        /// </param>
        /// <remarks>
        /// JPG's upload by user will likely have some compression
        /// Therefore a value of 40 or more will be needed to see some 
        /// improvements in file size
        /// </remarks>
        private static void SaveJpgWithCompression(Image original, string fileName, long compression)
        {
            // original:	The image to compress
            // fileName:	The filename to save the JPG to.
            // compression:	A value from 1 to 100 A value of 100 is no compression. 
            //				A value of 1 is maximum compression.

            // Note.  JPG's upload by user will likely have some compression
            // Therefore a value of 40 or more will be needed to see some 
            // improvements in file size
            EncoderParameters eps = new EncoderParameters(1);
            eps.Param[0] = new EncoderParameter(Encoder.Quality, compression);
            ImageCodecInfo ici = GetEncoderInfo("image/jpeg");
            original.Save(fileName, ici, eps);
        }
        //SaveJpgWithCompression


        /// <summary>
        /// Function to Compresses a JPEG image
        /// </summary>
        /// <param name="original">The image to compress</param>
        /// <param name="compression">
        /// A value from 1 to 100 A value of 100 is no compression. 
        /// A value of 1 is maximum compression.
        /// </param>
        /// <returns>
        /// JPG's upload by user will likely have some compression
        /// Therefore a value of 40 or more will be needed to see some 
        /// improvements in file size
        /// </returns>
        /// <remarks>return memorystream of the compressed image</remarks>

        private static MemoryStream SaveJpgWithCompression(Image original, long compression)
        {
            // original:	The image to compress
            // compression:	A value from 1 to 100 A value of 100 is no compression. 
            //				A value of 1 is maximum compression.
            // Note.  JPG's upload by user will likely have some compression
            // Therefore a value of 40 or more will be needed to see some 
            // improvements in file size
            MemoryStream ms = new MemoryStream();
            EncoderParameters eps = new EncoderParameters(1);
            eps.Param[0] = new EncoderParameter(Encoder.Quality, compression);

            ImageCodecInfo ici = GetEncoderInfo("image/jpeg");
            //original.Save ("C:\\atest\\compresstest.jpg",ici,eps);
            original.Save(ms, ici, eps);
            return ms;
        }
        //SaveJpgWithCompression


        /// <summary>
        /// Function to Compresses a BMP image
        /// </summary>
        /// <param name="original">The image to compress</param>
        /// <param name="compression">
        /// A value from 1 to 100 A value of 100 is no compression. 
        /// A value of 1 is maximum compression.
        /// </param>
        /// <returns>
        /// JPG's upload by user will likely have some compression
        /// Therefore a value of 40 or more will be needed to see some 
        /// improvements in file size
        /// </returns>
        /// <remarks>return memorystream of the compressed image</remarks>

        private static MemoryStream SaveBmpWithCompression(Image original, long compression)
        {
            // original:	The image to compress
            // compression:	A value from 1 to 100 A value of 100 is no compression. 
            //				A value of 1 is maximum compression.
            // Note.  JPG's upload by user will likely have some compression
            // Therefore a value of 40 or more will be needed to see some 
            // improvements in file size
            MemoryStream ms = new MemoryStream();
            EncoderParameters eps = new EncoderParameters(1);
            eps.Param[0] = new EncoderParameter(Encoder.Quality, compression);

            ImageCodecInfo ici = GetEncoderInfo("image/bmp");
            //original.Save ("C:\\atest\\compresstest.jpg",ici,eps);
            original.Save(ms, ici, eps);
            return ms;
        }
        //SaveJpgWithCompression



        //*********************************************************************
        //
        // GetTransparentColorPalette 方法
        //
        // Finds the transparent colors in a GIF image. 
        //
        //*********************************************************************
        /// <summary>
        /// Finds the transparent colors in a GIF image. 
        /// </summary>
        /// <param name="original">原始图片</param>
        /// <returns>return the colorpalette of the transparent color</returns>
        /// <remarks></remarks>
        private static ColorPalette GetTransparentColorPalette(Bitmap original)
        {
            Color testColor = default(Color);
            Color newColor = default(Color);
            ColorPalette pal = original.Palette;

            int i = 0;
            for (i = 0; i <= (pal.Entries.Length - 1) - 1; i++)
            {
                testColor = pal.Entries[i];

                if (testColor.A == 0)
                {
                    newColor = Color.FromArgb(0, testColor);
                    pal.Entries[i] = newColor;
                }
            }

            return pal;
        }
        //GetTransparentColorPalette




        //*********************************************************************
        //
        // GetEncoderInfo 方法
        //
        // Retrieves the the Image Codec for a particular image MIME type. 
        //
        //*********************************************************************

        /// <summary>
        /// Retrieves the the Image Codec for a particular image MIME type. 
        /// </summary>
        /// <param name="mimeType"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        private static ImageCodecInfo GetEncoderInfo(string mimeType)
        {
            // from KB article Q324790
            int j = 0;
            ImageCodecInfo[] encoders = null;
            encoders = ImageCodecInfo.GetImageEncoders();
            for (j = 0; j <= encoders.Length - 1; j++)
            {
                if (encoders[j].MimeType == mimeType)
                {
                    return encoders[j];
                }
            }
            return null;
        }
        //GetEncoderInfo



        //'*********************************************************************
        //'
        //' GetPartImageFromImageID 方法
        //'
        //' 由ImageID返回包含类图片二进制数据的Byte数组
        //' 注意：Byte数据类型用来保存 8 位（1 字节）无符号整数，值的范围为 0 到 255，默认值为 0
        //'
        //'*********************************************************************

        //Public Shared Function GetPartImageFromImageID(ByVal imageID As Integer) As SqlDataReader

        //    Dim connection As New SqlConnection(GlobalSetting.ConnectionString)
        //    Dim cmdGet As New SqlCommand("Mouldnet_GetPartImageFromImageID", connection)
        //    cmdGet.CommandType = CommandType.StoredProcedure
        //    cmdGet.Parameters.Add(New SqlParameter("@imageID", imageID))

        //    connection.Open()
        //    Return cmdGet.ExecuteReader(CommandBehavior.CloseConnection)

        //End Function


        //'*********************************************************************
        //'
        //' GetPartImageIDFromSubject 方法
        //'
        //' 由partID and SubjectID返回ImageID
        //'
        //'*********************************************************************

        //Public Shared Function GetPartImageIDFromSubject(ByVal partID As Integer, ByVal subjectID As Integer) As Integer

        //    Dim imgID As Integer = 0


        //    Dim connection As New SqlConnection(GlobalSetting.ConnectionString)
        //    Dim cmdGet As New SqlCommand("Mouldnet_GetPartImageIDFromSubject", connection)
        //    cmdGet.CommandType = CommandType.StoredProcedure
        //    cmdGet.Parameters.Add(New SqlParameter("@partID", partID))
        //    cmdGet.Parameters.Add(New SqlParameter("@subjectID", subjectID))

        //    connection.Open()
        //    Dim dr As SqlDataReader = cmdGet.ExecuteReader()

        //    If dr.Read Then
        //        imgID = dr.GetInt32(0)
        //    End If

        //    connection.Close()

        //    Return imgID

        //End Function

        //'*********************************************************************
        //'
        //' GetCategoryImageFromImageID 方法
        //'
        //' 由ImageID返回包含类图片二进制数据的Byte数组
        //' 注意：Byte数据类型用来保存 8 位（1 字节）无符号整数，值的范围为 0 到 255，默认值为 0
        //'
        //'*********************************************************************

        //Public Shared Function GetCategoryImageFromImageID(ByVal imageID As Integer) As SqlDataReader

        //    Dim connection As New SqlConnection(GlobalSetting.ConnectionString)
        //    Dim cmdGet As New SqlCommand("Mouldnet_GetCategoryImageFromImageID", connection)
        //    cmdGet.CommandType = CommandType.StoredProcedure
        //    cmdGet.Parameters.Add(New SqlParameter("@imageID", imageID))

        //    connection.Open()
        //    Return cmdGet.ExecuteReader(CommandBehavior.CloseConnection)

        //End Function


        //'*********************************************************************
        //'
        //' GetMFRImageFromImageID 方法
        //'
        //' 由ImageID返回包含类图片二进制数据的Byte数组
        //' 注意：Byte数据类型用来保存 8 位（1 字节）无符号整数，值的范围为 0 到 255，默认值为 0
        //'
        //'*********************************************************************

        //Public Shared Function GetMFRImageFromImageID(ByVal imageID As Integer) As SqlDataReader

        //    Dim connection As New SqlConnection(GlobalSetting.ConnectionString)
        //    Dim cmdGet As New SqlCommand("Mouldnet_GetMFRImageFromImageID", connection)
        //    cmdGet.CommandType = CommandType.StoredProcedure
        //    cmdGet.Parameters.Add(New SqlParameter("@imageID", imageID))

        //    connection.Open()
        //    Return cmdGet.ExecuteReader(CommandBehavior.CloseConnection)

        //End Function


        //'*********************************************************************
        //'
        //' GetPartDefaultImage 方法
        //'
        //' 返回一个Byte数据来包含类别默认图片的二进制数据
        //' 注意：Byte数据类型用来保存 8 位（1 字节）无符号整数，值的范围为 0 到 255，默认值为 0
        //'
        //'*********************************************************************

        //Public Shared Function GetPartSizedImage(ByVal imageID As Integer, ByVal width As Integer, ByVal height As Integer, ByVal isThumbnail As Boolean) As Byte()


        //    Dim image As Byte() = Nothing

        //    Dim contentType As String = String.Empty
        //    Dim returnWidth As Integer = -1
        //    Dim returnHeight As Integer = -1
        //    Dim returnThumbnail As Boolean = False


        //    Dim connection As New SqlConnection(GlobalSetting.ConnectionString)
        //    Dim commandGet As New SqlCommand("Mouldnet_GetPartImage", connection)
        //    commandGet.CommandType = CommandType.StoredProcedure


        //    If isThumbnail Then
        //        If width = -1 AndAlso height = -1 Then
        //            'just output thumbnail image data
        //            image = GetPartImageAsThumbnail(imageID)
        //        Else
        //            'retrive original image data
        //            commandGet.Parameters.Add(New SqlParameter("@imageID", imageID))
        //            commandGet.Parameters.Add(New SqlParameter("@width", width))
        //            commandGet.Parameters.Add(New SqlParameter("@height", height))
        //            commandGet.Parameters.Add(New SqlParameter("@isThumbnail", isThumbnail))

        //            connection.Open()
        //            Dim dr As SqlDataReader = commandGet.ExecuteReader()
        //            If dr.Read() Then
        //                contentType = CStr(dr("Image_contentType"))
        //                image = CType(dr("Image_imageData"), Byte())
        //                returnWidth = Fix(dr("Width"))
        //                returnHeight = Fix(dr("Height"))
        //                returnThumbnail = Convert.ToBoolean(dr("IsThumbnail"))
        //            End If
        //            connection.Close()


        //            image = ModifyImage(image, width, height, contentType, isThumbnail)

        //            'save modified image data as thumbnail into db
        //            If width <> -1 AndAlso height <> -1 Then

        //                Dim commandSave As New SqlCommand("Mouldnet_SaveSizedPartImage", connection)
        //                commandSave.CommandType = CommandType.StoredProcedure

        //                commandSave.Parameters.Add(New SqlParameter("@imageID", imageID))
        //                commandSave.Parameters.Add(New SqlParameter("@sizedImageWidth", width))
        //                commandSave.Parameters.Add(New SqlParameter("@sizedImageHeight", height))
        //                commandSave.Parameters.Add(New SqlParameter("@sizedImageIsThumbnail", isThumbnail))
        //                commandSave.Parameters.Add(New SqlParameter("@sizedImageData", SqlDbType.Image)).Value = image

        //                connection.Open()
        //                commandSave.ExecuteNonQuery()
        //                connection.Close()
        //            End If

        //        End If

        //        Return image

        //    Else
        //        'retrive original image data
        //        commandGet.Parameters.Add(New SqlParameter("@imageID", imageID))
        //        commandGet.Parameters.Add(New SqlParameter("@width", width))
        //        commandGet.Parameters.Add(New SqlParameter("@height", height))
        //        commandGet.Parameters.Add(New SqlParameter("@isThumbnail", isThumbnail))

        //        connection.Open()
        //        Dim dr As SqlDataReader = commandGet.ExecuteReader()
        //        If dr.Read() Then
        //            contentType = CStr(dr("Image_contentType"))
        //            image = CType(dr("Image_imageData"), Byte())
        //            returnWidth = Fix(dr("Width"))
        //            returnHeight = Fix(dr("Height"))
        //            returnThumbnail = Convert.ToBoolean(dr("IsThumbnail"))
        //        End If
        //        connection.Close()

        //        If width = -1 AndAlso height = -1 Then
        //            'output original image data
        //            Return image
        //        Else
        //            'output modified image data
        //            image = ModifyImage(image, width, height, contentType, isThumbnail)
        //            Return image
        //        End If

        //    End If

        //End Function



        //'*********************************************************************
        //'
        //' GetCategoryImageAsThumbnail 方法
        //'
        //' 直接读取调整大小后的图片，如果返回为DBNull，则函数返回Nothing
        //'
        //'*********************************************************************


        //Public Shared Function GetPartImageAsThumbnail(ByVal imageID As Integer) As Byte()


        //    Dim image As Byte() = Nothing


        //    Dim connection As New SqlConnection(GlobalSetting.ConnectionString)
        //    Dim commandGet As New SqlCommand("Mouldnet_GetPartImageAsThumbnail", connection)
        //    commandGet.CommandType = CommandType.StoredProcedure

        //    commandGet.Parameters.Add(New SqlParameter("@imageID", imageID))


        //    connection.Open()
        //    Dim dr As SqlDataReader = commandGet.ExecuteReader()

        //    If dr.Read() Then
        //        If IsDBNull(dr("Image_SizedImageData")) Then
        //            '如果为空，则返回默认大小的thumb图片
        //            image = GetPartSizedImage(imageID, -1, 50, True)
        //        Else
        //            image = CType(dr("Image_SizedImageData"), Byte())
        //        End If
        //    End If
        //    connection.Close()

        //    Return image

        //End Function



        //'*********************************************************************
        //'
        //' GetPartAllImages 方法
        //'
        //' Returns datareader containing stream record of Manufacturer images/logos. 
        //'
        //'*********************************************************************
        //Public Shared Function GetPartAllImages(ByVal partID As Integer) As SqlDataReader
        //    Dim connection As New SqlConnection(GlobalSetting.ConnectionString)
        //    Dim cmdGet As New SqlCommand("Mouldnet_GetPartAllImages", connection)
        //    cmdGet.CommandType = CommandType.StoredProcedure
        //    cmdGet.Parameters.Add(New SqlParameter("@partID", partID))

        //    connection.Open()
        //    Return cmdGet.ExecuteReader(CommandBehavior.CloseConnection)

        //End Function 'GetPortalImage



        //'*********************************************************************
        //'
        //' GetPartAllSizedImages 方法
        //'
        //' 返回一个Byte数据来包含种类所有大小已调整的图片二进制数据列表
        //'
        //'*********************************************************************

        //Public Shared Function GetPartAllSizedImages(ByVal partID As Integer) As Dictionary(Of Integer, Byte())

        //    Dim imageDictionary As New Dictionary(Of Integer, Byte())



        //    Dim connection As New SqlConnection(GlobalSetting.ConnectionString)
        //    Dim cmdGet As New SqlCommand("Mouldnet_GetPartAllSizedImages", connection)
        //    cmdGet.CommandType = CommandType.StoredProcedure

        //    cmdGet.Parameters.Add(New SqlParameter("@partID", partID))

        //    connection.Open()
        //    Dim dr As SqlDataReader = cmdGet.ExecuteReader(CommandBehavior.CloseConnection)

        //    Do While dr.Read


        //        imageDictionary.Add(Fix(dr("Image_ID")), TryCast(dr("Image_imageData"), Byte()))

        //    Loop
        //    dr.Close()

        //    Return imageDictionary

        //End Function 'GetModuleImage





        //'*********************************************************************
        //'
        //' GetCategoryImage 方法
        //'
        //' 返回一个Byte数据来包含类别默认图片的二进制数据
        //' 注意：Byte数据类型用来保存 8 位（1 字节）无符号整数，值的范围为 0 到 255，默认值为 0
        //'
        //'*********************************************************************

        //Public Shared Function GetCategorySizedImage(ByVal imageID As Integer, ByVal width As Integer, ByVal height As Integer, ByVal isThumbnail As Boolean) As Byte()


        //    Dim image As Byte() = Nothing

        //    Dim contentType As String = String.Empty
        //    Dim returnWidth As Integer = -1
        //    Dim returnHeight As Integer = -1
        //    Dim returnThumbnail As Boolean = False


        //    Dim connection As New SqlConnection(GlobalSetting.ConnectionString)
        //    Dim commandGet As New SqlCommand("Mouldnet_GetCategoryImage", connection)
        //    commandGet.CommandType = CommandType.StoredProcedure


        //    commandGet.Parameters.Add(New SqlParameter("@imageID", imageID))
        //    commandGet.Parameters.Add(New SqlParameter("@width", width))
        //    commandGet.Parameters.Add(New SqlParameter("@height", height))
        //    commandGet.Parameters.Add(New SqlParameter("@isThumbnail", isThumbnail))


        //    connection.Open()
        //    Dim dr As SqlDataReader = commandGet.ExecuteReader()
        //    If dr.Read() Then
        //        contentType = CStr(dr("Image_contentType"))
        //        image = CType(dr("Image_imageData"), Byte())
        //        returnWidth = Fix(dr("Width"))
        //        returnHeight = Fix(dr("Height"))
        //        returnThumbnail = Convert.ToBoolean(dr("IsThumbnail"))
        //    End If
        //    connection.Close()


        //    ' if no resize, just return
        //    If returnWidth = width AndAlso returnHeight = height AndAlso returnThumbnail = isThumbnail Then
        //        Return image
        //    End If
        //    ' Perform resize
        //    image = ModifyImage(image, width, height, contentType, isThumbnail)

        //    ' Save Resized Image
        //    Dim commandSave As New SqlCommand("Mouldnet_SaveSizedCategoryImage", connection)
        //    commandSave.CommandType = CommandType.StoredProcedure

        //    commandSave.Parameters.Add(New SqlParameter("@imageID", imageID))
        //    commandSave.Parameters.Add(New SqlParameter("@sizedImageWidth", width))
        //    commandSave.Parameters.Add(New SqlParameter("@sizedImageHeight", height))
        //    commandSave.Parameters.Add(New SqlParameter("@sizedImageIsThumbnail", isThumbnail))
        //    commandSave.Parameters.Add(New SqlParameter("@sizedImageData", SqlDbType.Image)).Value = image

        //    connection.Open()
        //    commandSave.ExecuteNonQuery()
        //    connection.Close()

        //    Return image

        //End Function



        //'*********************************************************************
        //'
        //' GetCategoryImageAsThumbnail 方法
        //'
        //' 直接读取调整大小后的图片，如果返回为DBNull，则函数返回Nothing
        //'
        //'*********************************************************************


        //Public Shared Function GetCategoryImageAsThumbnail(ByVal imageID As Integer) As Byte()


        //    Dim image As Byte() = Nothing


        //    Dim connection As New SqlConnection(GlobalSetting.ConnectionString)
        //    Dim commandGet As New SqlCommand("Mouldnet_GetCategoryImageAsThumbnail", connection)
        //    commandGet.CommandType = CommandType.StoredProcedure

        //    commandGet.Parameters.Add(New SqlParameter("@imageID", imageID))


        //    connection.Open()
        //    Dim dr As SqlDataReader = commandGet.ExecuteReader()

        //    If dr.Read() Then
        //        If IsDBNull(dr("Image_SizedImageData")) Then
        //            image = GetCategorySizedImage(imageID, -1, 50, True)
        //        Else
        //            image = CType(dr("Image_SizedImageData"), Byte())
        //        End If
        //    End If
        //    connection.Close()

        //    Return image

        //End Function



        //'*********************************************************************
        //'
        //' GetCategoryAllImages 方法
        //'
        //' Returns datareader containing stream record of Manufacturer images/logos. 
        //'
        //'*********************************************************************
        //Public Shared Function GetCategoryAllImages(ByVal categoryID As Integer) As SqlDataReader
        //    Dim connection As New SqlConnection(GlobalSetting.ConnectionString)
        //    Dim cmdGet As New SqlCommand("Mouldnet_GetCategoryAllImages", connection)
        //    cmdGet.CommandType = CommandType.StoredProcedure
        //    cmdGet.Parameters.Add(New SqlParameter("@categoryID", categoryID))

        //    connection.Open()
        //    Return cmdGet.ExecuteReader(CommandBehavior.CloseConnection)

        //End Function 'GetPortalImage



        //'*********************************************************************
        //'
        //' GetCategoryAllSizedImages 方法
        //'
        //' 返回一个Byte数据来包含种类所有大小已调整的图片二进制数据列表
        //'
        //'*********************************************************************

        //Public Shared Function GetCategoryAllSizedImages(ByVal categoryID As Integer) As Dictionary(Of Integer, Byte())

        //    Dim imageDictionary As New Dictionary(Of Integer, Byte())



        //    Dim connection As New SqlConnection(GlobalSetting.ConnectionString)
        //    Dim cmdGet As New SqlCommand("Mouldnet_GetCategoryAllSizedImages", connection)
        //    cmdGet.CommandType = CommandType.StoredProcedure

        //    cmdGet.Parameters.Add(New SqlParameter("@categoryID", categoryID))

        //    connection.Open()
        //    Dim dr As SqlDataReader = cmdGet.ExecuteReader(CommandBehavior.CloseConnection)

        //    Do While dr.Read

        //        imageDictionary.Add(Fix(dr("Image_ID")), TryCast(dr("Image_imageData"), Byte()))
        //    Loop
        //    dr.Close()

        //    Return imageDictionary

        //End Function 'GetModuleImage




        //Public Shared Function GetModuleImage(ByVal moduleID As Integer, ByVal imageID As Integer, ByVal width As Integer, ByVal height As Integer, ByVal isThumbnail As Boolean) As Byte()
        //    Dim contentType As String = String.Empty
        //    Dim image As Byte() = Nothing
        //    Dim returnWidth As Integer = -1
        //    Dim returnHeight As Integer = -1
        //    Dim returnThumbnail As Boolean = False

        //    Dim conPortal As New SqlConnection(GlobalSetting.ConnectionString)
        //    Dim cmdGet As New SqlCommand("Mouldnet_ImagesGetModuleImage", conPortal)
        //    cmdGet.CommandType = CommandType.StoredProcedure

        //    cmdGet.Parameters.Add(New SqlParameter("@portalID", GlobalSetting.PortalID))
        //    cmdGet.Parameters.Add(New SqlParameter("@moduleID", moduleID))
        //    cmdGet.Parameters.Add(New SqlParameter("@imageID", imageID))
        //    cmdGet.Parameters.Add(New SqlParameter("@width", width))
        //    cmdGet.Parameters.Add(New SqlParameter("@height", height))
        //    cmdGet.Parameters.Add(New SqlParameter("@isThumbnail", isThumbnail))

        //    conPortal.Open()
        //    Dim dr As SqlDataReader = cmdGet.ExecuteReader()
        //    If dr.Read() Then
        //        contentType = CStr(dr("Image_contentType"))
        //        image = CType(dr("Image_imageData"), Byte())
        //        returnWidth = Fix(dr("width"))
        //        returnHeight = Fix(dr("height"))
        //        returnThumbnail = Convert.ToBoolean(dr("isThumbnail"))
        //    End If
        //    conPortal.Close()

        //    ' if no resize, just return
        //    If returnWidth = width AndAlso returnHeight = height AndAlso returnThumbnail = isThumbnail Then
        //        Return image
        //    End If
        //    ' Perform resize
        //    image = ModifyImage(image, width, height, contentType, isThumbnail)

        //    ' Save Resized Image
        //    Dim cmdSave As New SqlCommand("Mouldnet_ImagesSaveSizedModuleImage", conPortal)
        //    cmdSave.CommandType = CommandType.StoredProcedure

        //    cmdSave.Parameters.Add(New SqlParameter("@portalID", GlobalSetting.PortalID))
        //    cmdSave.Parameters.Add(New SqlParameter("@imageID", imageID))
        //    cmdSave.Parameters.Add(New SqlParameter("@sizedImageWidth", width))
        //    cmdSave.Parameters.Add(New SqlParameter("@sizedImageHeight", height))
        //    cmdSave.Parameters.Add(New SqlParameter("@sizedImageIsThumbnail", isThumbnail))
        //    cmdSave.Parameters.Add(New SqlParameter("@sizedImageData", SqlDbType.Image)).Value = image

        //    conPortal.Open()
        //    cmdSave.ExecuteNonQuery()
        //    conPortal.Close()

        //    Return image

        //End Function 'GetModuleImage


        //Public Shared Function GetManufacturerSizedImage(ByVal imageID As Integer, ByVal width As Integer, ByVal height As Integer, ByVal isThumbnail As Boolean) As Byte()


        //    Dim image As Byte() = Nothing

        //    Dim contentType As String = String.Empty
        //    Dim returnWidth As Integer = -1
        //    Dim returnHeight As Integer = -1
        //    Dim returnThumbnail As Boolean = False



        //    Dim connection As New SqlConnection(GlobalSetting.ConnectionString)
        //    Dim commandGet As New SqlCommand("Mouldnet_GetManufacturerImage", connection)
        //    commandGet.CommandType = CommandType.StoredProcedure


        //    commandGet.Parameters.Add(New SqlParameter("@imageID", imageID))
        //    commandGet.Parameters.Add(New SqlParameter("@width", width))
        //    commandGet.Parameters.Add(New SqlParameter("@height", height))
        //    commandGet.Parameters.Add(New SqlParameter("@isThumbnail", isThumbnail))


        //    connection.Open()
        //    Dim dr As SqlDataReader = commandGet.ExecuteReader()
        //    If dr.Read() Then
        //        contentType = CStr(dr("Image_contentType"))
        //        image = CType(dr("Image_imageData"), Byte())
        //        returnWidth = Fix(dr("Width"))
        //        returnHeight = Fix(dr("Height"))
        //        returnThumbnail = Convert.ToBoolean(dr("IsThumbnail"))
        //    End If
        //    connection.Close()


        //    ' if no resize, just return normal image, when width=-1 and height =-1 and isThumbnail =False
        //    If returnWidth = width AndAlso returnHeight = height AndAlso returnThumbnail = isThumbnail Then
        //        Return image
        //    End If
        //    ' Perform resize
        //    image = ModifyImage(image, width, height, contentType, isThumbnail)

        //    ' Save Resized Image
        //    Dim commandSave As New SqlCommand("Mouldnet_SaveSizedManufacturerImage", connection)
        //    commandSave.CommandType = CommandType.StoredProcedure

        //    commandSave.Parameters.Add(New SqlParameter("@imageID", imageID))
        //    commandSave.Parameters.Add(New SqlParameter("@sizedImageWidth", width))
        //    commandSave.Parameters.Add(New SqlParameter("@sizedImageHeight", height))
        //    commandSave.Parameters.Add(New SqlParameter("@sizedImageIsThumbnail", isThumbnail))
        //    commandSave.Parameters.Add(New SqlParameter("@sizedImageData", SqlDbType.Image)).Value = image

        //    connection.Open()
        //    commandSave.ExecuteNonQuery()
        //    connection.Close()

        //    Return image

        //End Function



        //'*********************************************************************
        //'
        //' GetManufacturerImageAsThumbnail 方法
        //'
        //' 直接读取调整大小后的图片，如果返回为DBNull，则函数返回Nothing
        //'
        //'*********************************************************************


        //Public Shared Function GetManufacturerImageAsThumbnail(ByVal imageID As Integer) As Byte()


        //    Dim image As Byte() = Nothing

        //    Dim connection As New SqlConnection(GlobalSetting.ConnectionString)
        //    Dim commandGet As New SqlCommand("Mouldnet_GetManufacturerImageAsThumbnail", connection)
        //    commandGet.CommandType = CommandType.StoredProcedure

        //    commandGet.Parameters.Add(New SqlParameter("@imageID", imageID))


        //    connection.Open()
        //    Dim dr As SqlDataReader = commandGet.ExecuteReader()
        //    If dr.Read() Then
        //        If IsDBNull(dr("Image_SizedImageData")) Then
        //            image = GetManufacturerSizedImage(imageID, -1, 50, True)
        //        Else
        //            image = CType(dr("Image_SizedImageData"), Byte())
        //        End If
        //    End If
        //    connection.Close()

        //    Return image

        //End Function


        //'*********************************************************************
        //'
        //' GetManufacturerImage 方法
        //'
        //' Returns datareader containing stream record of Manufacturer images/logos. 
        //'
        //'*********************************************************************
        //Public Shared Function GetManufacturerAllImages(ByVal mfrID As Integer) As SqlDataReader
        //    Dim connection As New SqlConnection(GlobalSetting.ConnectionString)
        //    Dim cmdGet As New SqlCommand("Mouldnet_GetManufacturerAllImages", connection)
        //    cmdGet.CommandType = CommandType.StoredProcedure
        //    cmdGet.Parameters.Add(New SqlParameter("@mfrID", mfrID))

        //    connection.Open()
        //    Return cmdGet.ExecuteReader(CommandBehavior.CloseConnection)

        //End Function 'GetPortalImage


        //'*********************************************************************
        //'
        //' GetCategoryImage 方法
        //'
        //' 返回一个Byte数据来包含种类所有大小已调整的图片二进制数据列表
        //'
        //'*********************************************************************

        //Public Shared Function GetManufactuerAllSizedImages(ByVal mfrID As Integer) As Dictionary(Of Integer, Byte())

        //    Dim imageDictionary As New Dictionary(Of Integer, Byte())



        //    Dim connection As New SqlConnection(GlobalSetting.ConnectionString)
        //    Dim cmdGet As New SqlCommand("Mouldnet_GetManufacturerAllSizedImages", connection)
        //    cmdGet.CommandType = CommandType.StoredProcedure

        //    cmdGet.Parameters.Add(New SqlParameter("@mfrID", mfrID))

        //    connection.Open()
        //    Dim dr As SqlDataReader = cmdGet.ExecuteReader(CommandBehavior.CloseConnection)

        //    Do While dr.Read

        //        imageDictionary.Add(Fix(dr("Image_ID")), TryCast(dr("Image_imageData"), Byte()))
        //    Loop
        //    dr.Close()

        //    Return imageDictionary

        //End Function 'GetModuleImage




        // ''*********************************************************************
        // ''
        // '' GetPortalImage 方法
        // ''
        // '' Returns a byte array containing a community image. 
        // ''
        // ''*********************************************************************
        //Public Shared Function GetPortalImage(ByVal fileName As String) As SqlDataReader
        //    Dim conPortal As New SqlConnection(GlobalSetting.ConnectionString)
        //    Dim cmdGet As New SqlCommand("Mouldnet_ImagesGetPortalImage", conPortal)
        //    cmdGet.CommandType = CommandType.StoredProcedure
        //    cmdGet.Parameters.Add(New SqlParameter("@portalID", GlobalSetting.PortalID))
        //    cmdGet.Parameters.Add(New SqlParameter("@fileName", fileName))

        //    conPortal.Open()
        //    Return cmdGet.ExecuteReader(CommandBehavior.CloseConnection)
        //End Function 'GetPortalImage




        // ''*********************************************************************
        // ''
        // '' GetAllPortalImages 方法
        // ''
        // '' Returns a list of all the images in this community. 
        // ''
        // ''*********************************************************************
        //Public Shared Function GetAllPortalImages() As DataSet
        //    Dim dadGet As New SqlDataAdapter("Mouldnet_ImagesGetPortalImages", GlobalSetting.ConnectionString)
        //    dadGet.SelectCommand.CommandType = CommandType.StoredProcedure
        //    dadGet.SelectCommand.Parameters.Add(New SqlParameter("@portalID", GlobalSetting.PortalID))

        //    Dim dstImages As New DataSet
        //    dadGet.Fill(dstImages)
        //    Return dstImages
        //End Function 'GetAllPortalImages




        // ''*********************************************************************
        // ''
        // '' AddPortalImage 方法
        // ''
        // '' Adds a new community image to the database. 
        // ''
        // ''*********************************************************************
        //Public Shared Function AddPortalImage(ByVal imageType As GlobalImageType, ByVal proposedFile As HttpPostedFile) As String
        //    ' Make sure that the posted file has content
        //    If proposedFile Is Nothing OrElse proposedFile.ContentLength = 0 Then
        //        Return Nothing
        //    End If

        //    Dim imgFileName As String = proposedFile.FileName.Remove(0, proposedFile.FileName.LastIndexOf("\") + 1)
        //    Dim imgContentType As String = proposedFile.ContentType

        //    ' Make sure that the posted file is actually an image
        //    If Not IsImageFile(imgFileName) Then
        //        Return Nothing
        //    End If

        //    ' Process the image into byte array
        //    Dim imgStream As Stream = proposedFile.InputStream
        //    Dim imgLen As Integer = proposedFile.ContentLength
        //    Dim imgBinaryData(imgLen) As Byte
        //    Dim n As Integer = imgStream.Read(imgBinaryData, 0, imgLen)

        //    ' Add the image to the database
        //    Dim conPortal As New SqlConnection(GlobalSetting.ConnectionString)
        //    Dim cmdAdd As New SqlCommand("Mouldnet_ImagesAddPortalImage", conPortal)
        //    cmdAdd.CommandType = CommandType.StoredProcedure

        //    cmdAdd.Parameters.Add(New SqlParameter("@RETURN_VALUE", SqlDbType.Int)).Direction = ParameterDirection.ReturnValue
        //    cmdAdd.Parameters.Add(New SqlParameter("@portalID", GlobalSetting.PortalID))
        //    cmdAdd.Parameters.Add(New SqlParameter("@fileName", imgFileName))
        //    cmdAdd.Parameters.Add(New SqlParameter("@contentType", imgContentType))
        //    cmdAdd.Parameters.Add(New SqlParameter("@imageType", imageType))
        //    cmdAdd.Parameters.Add(New SqlParameter("@imageData", SqlDbType.Image)).Value = imgBinaryData

        //    conPortal.Open()
        //    Dim result As Integer = cmdAdd.ExecuteNonQuery()
        //    conPortal.Close()

        //    Return imgFileName
        //End Function 'AddPortalImage




        // ''*********************************************************************
        // ''
        // '' DeletePortalImage 方法
        // ''
        // '' Removes a community image from the database. 
        // ''
        // ''*********************************************************************
        //Public Shared Sub DeletePortalImage(ByVal imageName As String)
        //    Dim conPortal As New SqlConnection(GlobalSetting.ConnectionString)
        //    Dim cmdDelete As New SqlCommand("Mouldnet_ImagesDeletePortalImage", conPortal)
        //    cmdDelete.CommandType = CommandType.StoredProcedure

        //    cmdDelete.Parameters.Add(New SqlParameter("@portalID", GlobalSetting.PortalID))
        //    cmdDelete.Parameters.Add(New SqlParameter("@fileName", imageName))

        //    conPortal.Open()
        //    cmdDelete.ExecuteNonQuery()
        //    conPortal.Close()
        //End Sub 'DeletePortalImage


        //'*********************************************************************
        //'
        //' AddCategoryImage 方法
        //'
        //' Adds a category image to the database. 
        //'
        //'*********************************************************************
        //Public Shared Function AddPartImage(ByVal partID As Integer, ByVal imgSubjectID As Integer, ByVal proposedFile As HttpPostedFile) As Integer
        //    ' Make sure that the posted file has content
        //    If proposedFile Is Nothing OrElse proposedFile.ContentLength = 0 Then
        //        Return -1
        //    End If
        //    Dim imgFileName As String = proposedFile.FileName.Remove(0, proposedFile.FileName.LastIndexOf("\") + 1)
        //    Dim imgContentType As String = proposedFile.ContentType


        //    ' Make sure that the posted file is actually an image
        //    If Not IsImageFile(imgFileName) Then
        //        Return -1
        //    End If
        //    ' Process the image into byte array
        //    Dim imgStream As Stream = proposedFile.InputStream
        //    Dim imgLen As Integer = proposedFile.ContentLength
        //    Dim imgBinaryData(imgLen) As Byte
        //    Dim n As Integer = imgStream.Read(imgBinaryData, 0, imgLen)

        //    ' Add the image to the database
        //    Dim connection As New SqlConnection(GlobalSetting.ConnectionString)
        //    Dim commandAdd As New SqlCommand("Mouldnet_AddPartImage", connection)
        //    commandAdd.CommandType = CommandType.StoredProcedure

        //    commandAdd.Parameters.Add(New SqlParameter("@RETURN_VALUE", SqlDbType.Int)).Direction = ParameterDirection.ReturnValue
        //    commandAdd.Parameters.Add(New SqlParameter("@partID", partID))
        //    commandAdd.Parameters.Add(New SqlParameter("@subjectID", imgSubjectID))
        //    commandAdd.Parameters.Add(New SqlParameter("@imageName", imgFileName))
        //    commandAdd.Parameters.Add(New SqlParameter("@contentType", imgContentType))
        //    commandAdd.Parameters.Add(New SqlParameter("@imageData", SqlDbType.Image)).Value = imgBinaryData

        //    connection.Open()
        //    commandAdd.ExecuteNonQuery()
        //    Dim result As Integer = Fix(commandAdd.Parameters("@RETURN_VALUE").Value)
        //    connection.Close()

        //    Return result
        //End Function 'AddModuleImage




        //'*********************************************************************
        //'
        //' UpdateCategoryImage 方法
        //'
        //' Updates a category image in the database. 
        //'
        //'*********************************************************************
        //Public Shared Sub UpdatePartImage(ByVal imageID As Integer, ByVal imgSubjectID As Integer, ByVal proposedFile As HttpPostedFile)
        //    ' Make sure that the posted file has content
        //    If proposedFile Is Nothing OrElse proposedFile.ContentLength = 0 Then
        //        Return
        //    End If
        //    Dim imgFileName As String = proposedFile.FileName.Remove(0, proposedFile.FileName.LastIndexOf("\") + 1)
        //    Dim imgContentType As String = proposedFile.ContentType


        //    ' Make sure that the posted file is actually an image
        //    If Not IsImageFile(imgFileName) Then
        //        Return
        //    End If
        //    ' Process the image into byte array
        //    Dim imgStream As Stream = proposedFile.InputStream
        //    Dim imgLen As Integer = proposedFile.ContentLength
        //    Dim imgBinaryData(imgLen) As Byte
        //    Dim n As Integer = imgStream.Read(imgBinaryData, 0, imgLen)

        //    ' Update the image in the database
        //    Dim connection As New SqlConnection(GlobalSetting.ConnectionString)
        //    Dim commandAdd As New SqlCommand("Mouldnet_UpdatePartImage", connection)
        //    commandAdd.CommandType = CommandType.StoredProcedure

        //    commandAdd.Parameters.Add(New SqlParameter("@RETURN_VALUE", SqlDbType.Int)).Direction = ParameterDirection.ReturnValue
        //    commandAdd.Parameters.Add(New SqlParameter("@imageID", imageID))
        //    commandAdd.Parameters.Add(New SqlParameter("@subjectID", imgSubjectID))
        //    commandAdd.Parameters.Add(New SqlParameter("@imageName", imgFileName))
        //    commandAdd.Parameters.Add(New SqlParameter("@contentType", imgContentType))
        //    commandAdd.Parameters.Add(New SqlParameter("@imageData", SqlDbType.Image)).Value = imgBinaryData

        //    connection.Open()
        //    commandAdd.ExecuteNonQuery()
        //    connection.Close()

        //End Sub 'UpdateModuleImage




        //'*********************************************************************
        //'
        //' DeleteCategoryImage 方法
        //'
        //' Removes a community image from the database. 
        //'

        //'*********************************************************************
        //Public Shared Sub DeletePartImage(ByVal imageID As String)
        //    Dim connection As New SqlConnection(GlobalSetting.ConnectionString)
        //    Dim commandDelete As New SqlCommand("Mouldnet_DeletePartImage", connection)
        //    commandDelete.CommandType = CommandType.StoredProcedure

        //    commandDelete.Parameters.Add(New SqlParameter("@imageID", imageID))

        //    connection.Open()
        //    commandDelete.ExecuteNonQuery()
        //    connection.Close()


        //End Sub 'DeletePortalImage



        //'*********************************************************************
        //'
        //' AddCategoryImage 方法
        //'
        //' Adds a category image to the database. 
        //'
        //'*********************************************************************
        //Public Shared Function AddCategoryImage(ByVal categoryID As Integer, ByVal proposedFile As HttpPostedFile) As Integer
        //    ' Make sure that the posted file has content
        //    If proposedFile Is Nothing OrElse proposedFile.ContentLength = 0 Then
        //        Return -1
        //    End If
        //    Dim imgFileName As String = proposedFile.FileName.Remove(0, proposedFile.FileName.LastIndexOf("\") + 1)
        //    Dim imgContentType As String = proposedFile.ContentType


        //    ' Make sure that the posted file is actually an image
        //    If Not IsImageFile(imgFileName) Then
        //        Return -1
        //    End If
        //    ' Process the image into byte array
        //    Dim imgStream As Stream = proposedFile.InputStream
        //    Dim imgLen As Integer = proposedFile.ContentLength
        //    Dim imgBinaryData(imgLen) As Byte
        //    Dim n As Integer = imgStream.Read(imgBinaryData, 0, imgLen)

        //    ' Add the image to the database
        //    Dim connection As New SqlConnection(GlobalSetting.ConnectionString)
        //    Dim commandAdd As New SqlCommand("Mouldnet_AddCategoryImage", connection)
        //    commandAdd.CommandType = CommandType.StoredProcedure

        //    commandAdd.Parameters.Add(New SqlParameter("@RETURN_VALUE", SqlDbType.Int)).Direction = ParameterDirection.ReturnValue
        //    commandAdd.Parameters.Add(New SqlParameter("@categoryID", categoryID))
        //    commandAdd.Parameters.Add(New SqlParameter("@imageName", imgFileName))
        //    commandAdd.Parameters.Add(New SqlParameter("@contentType", imgContentType))
        //    commandAdd.Parameters.Add(New SqlParameter("@imageData", SqlDbType.Image)).Value = imgBinaryData

        //    connection.Open()
        //    commandAdd.ExecuteNonQuery()
        //    Dim result As Integer = Fix(commandAdd.Parameters("@RETURN_VALUE").Value)
        //    connection.Close()

        //    Return result
        //End Function 'AddModuleImage




        //'*********************************************************************
        //'
        //' UpdateCategoryImage 方法
        //'
        //' Updates a category image in the database. 
        //'
        //'*********************************************************************
        //Public Shared Sub UpdateCategoryImage(ByVal imageID As Integer, ByVal proposedFile As HttpPostedFile)
        //    ' Make sure that the posted file has content
        //    If proposedFile Is Nothing OrElse proposedFile.ContentLength = 0 Then
        //        Return
        //    End If
        //    Dim imgFileName As String = proposedFile.FileName.Remove(0, proposedFile.FileName.LastIndexOf("\") + 1)
        //    Dim imgContentType As String = proposedFile.ContentType


        //    ' Make sure that the posted file is actually an image
        //    If Not IsImageFile(imgFileName) Then
        //        Return
        //    End If
        //    ' Process the image into byte array
        //    Dim imgStream As Stream = proposedFile.InputStream
        //    Dim imgLen As Integer = proposedFile.ContentLength
        //    Dim imgBinaryData(imgLen) As Byte
        //    Dim n As Integer = imgStream.Read(imgBinaryData, 0, imgLen)

        //    ' Update the image in the database
        //    Dim connection As New SqlConnection(GlobalSetting.ConnectionString)
        //    Dim commandAdd As New SqlCommand("Mouldnet_UpdateCategoryImage", connection)
        //    commandAdd.CommandType = CommandType.StoredProcedure

        //    commandAdd.Parameters.Add(New SqlParameter("@RETURN_VALUE", SqlDbType.Int)).Direction = ParameterDirection.ReturnValue
        //    commandAdd.Parameters.Add(New SqlParameter("@imageID", imageID))
        //    commandAdd.Parameters.Add(New SqlParameter("@imageName", imgFileName))
        //    commandAdd.Parameters.Add(New SqlParameter("@contentType", imgContentType))
        //    commandAdd.Parameters.Add(New SqlParameter("@imageData", SqlDbType.Image)).Value = imgBinaryData

        //    connection.Open()
        //    commandAdd.ExecuteNonQuery()
        //    connection.Close()

        //End Sub 'UpdateModuleImage




        //'*********************************************************************
        //'
        //' DeleteCategoryImage 方法
        //'
        //' Removes a community image from the database. 
        //'

        //'*********************************************************************
        //Public Shared Sub DeleteCategoryImage(ByVal imageID As String)
        //    Dim connection As New SqlConnection(GlobalSetting.ConnectionString)
        //    Dim commandDelete As New SqlCommand("Mouldnet_DeleteCategoryImage", connection)
        //    commandDelete.CommandType = CommandType.StoredProcedure

        //    commandDelete.Parameters.Add(New SqlParameter("@imageID", imageID))

        //    connection.Open()
        //    commandDelete.ExecuteNonQuery()
        //    connection.Close()


        //End Sub 'DeletePortalImage




        //'*********************************************************************
        //'
        //' AddManufacturerImage 方法
        //'
        //' Adds a Manufacturer image to the database. 
        //'
        //'*********************************************************************
        //Public Shared Function AddManufacturerImage(ByVal mfrID As Integer, ByVal proposedFile As HttpPostedFile) As Integer
        //    ' Make sure that the posted file has content
        //    If proposedFile Is Nothing OrElse proposedFile.ContentLength = 0 Then
        //        Return -1
        //    End If
        //    Dim imgFileName As String = proposedFile.FileName.Remove(0, proposedFile.FileName.LastIndexOf("\") + 1)
        //    Dim imgContentType As String = proposedFile.ContentType


        //    ' Make sure that the posted file is actually an image
        //    If Not IsImageFile(imgFileName) Then
        //        Return -1
        //    End If
        //    ' Process the image into byte array
        //    Dim imgStream As Stream = proposedFile.InputStream
        //    Dim imgLen As Integer = proposedFile.ContentLength
        //    Dim imgBinaryData(imgLen) As Byte
        //    Dim n As Integer = imgStream.Read(imgBinaryData, 0, imgLen)

        //    ' Add the image to the database
        //    Dim connection As New SqlConnection(GlobalSetting.ConnectionString)
        //    Dim commandAdd As New SqlCommand("Mouldnet_AddManufacturerImage", connection)
        //    commandAdd.CommandType = CommandType.StoredProcedure

        //    commandAdd.Parameters.Add(New SqlParameter("@RETURN_VALUE", SqlDbType.Int)).Direction = ParameterDirection.ReturnValue
        //    commandAdd.Parameters.Add(New SqlParameter("@mfrID", mfrID))
        //    commandAdd.Parameters.Add(New SqlParameter("@imageName", imgFileName))
        //    commandAdd.Parameters.Add(New SqlParameter("@contentType", imgContentType))
        //    commandAdd.Parameters.Add(New SqlParameter("@imageData", SqlDbType.Image)).Value = imgBinaryData

        //    connection.Open()
        //    commandAdd.ExecuteNonQuery()
        //    Dim result As Integer = Fix(commandAdd.Parameters("@RETURN_VALUE").Value)
        //    connection.Close()

        //    Return result
        //End Function 'AddModuleImage




        //'*********************************************************************
        //'
        //' UpdateManufacturerImage 方法
        //'
        //' Updates a Manufacturer image in the database. 
        //'
        //'*********************************************************************
        //Public Shared Sub UpdateManufacturerImage(ByVal imageID As Integer, ByVal proposedFile As HttpPostedFile)
        //    ' Make sure that the posted file has content
        //    If proposedFile Is Nothing OrElse proposedFile.ContentLength = 0 Then
        //        Return
        //    End If
        //    Dim imgFileName As String = proposedFile.FileName.Remove(0, proposedFile.FileName.LastIndexOf("\") + 1)
        //    Dim imgContentType As String = proposedFile.ContentType


        //    ' Make sure that the posted file is actually an image
        //    If Not IsImageFile(imgFileName) Then
        //        Return
        //    End If
        //    ' Process the image into byte array
        //    Dim imgStream As Stream = proposedFile.InputStream
        //    Dim imgLen As Integer = proposedFile.ContentLength
        //    Dim imgBinaryData(imgLen) As Byte
        //    Dim n As Integer = imgStream.Read(imgBinaryData, 0, imgLen)

        //    ' Update the image in the database
        //    Dim connection As New SqlConnection(GlobalSetting.ConnectionString)
        //    Dim commandAdd As New SqlCommand("Mouldnet_UpdateManufacturerImage", connection)
        //    commandAdd.CommandType = CommandType.StoredProcedure

        //    commandAdd.Parameters.Add(New SqlParameter("@RETURN_VALUE", SqlDbType.Int)).Direction = ParameterDirection.ReturnValue
        //    commandAdd.Parameters.Add(New SqlParameter("@imageID", imageID))
        //    commandAdd.Parameters.Add(New SqlParameter("@imageName", imgFileName))
        //    commandAdd.Parameters.Add(New SqlParameter("@contentType", imgContentType))
        //    commandAdd.Parameters.Add(New SqlParameter("@imageData", SqlDbType.Image)).Value = imgBinaryData

        //    connection.Open()
        //    commandAdd.ExecuteNonQuery()
        //    connection.Close()

        //End Sub 'UpdateModuleImage



        //'*********************************************************************
        //'
        //' DeleteManufacturerImage 方法
        //'
        //' Removes a manufacturer image from the database. 
        //'
        //'*********************************************************************
        //Public Shared Sub DeleteManufacturerImage(ByVal imageID As String)
        //    Dim connection As New SqlConnection(GlobalSetting.ConnectionString)
        //    Dim commandDelete As New SqlCommand("Mouldnet_DeleteManufacturerImage", connection)
        //    commandDelete.CommandType = CommandType.StoredProcedure

        //    commandDelete.Parameters.Add(New SqlParameter("@imageID", imageID))

        //    connection.Open()
        //    commandDelete.ExecuteNonQuery()
        //    connection.Close()


        //End Sub 'DeletePortalImage



        // ''*********************************************************************
        // ''
        // '' AddModuleImage 方法
        // ''
        // '' Adds a section image to the database. 
        // ''
        // ''*********************************************************************
        //Public Shared Function AddModuleImage(ByVal modulePageID As Integer, ByVal proposedFile As HttpPostedFile) As Integer
        //    ' Make sure that the posted file has content
        //    If proposedFile Is Nothing OrElse proposedFile.ContentLength = 0 Then
        //        Return -1
        //    End If
        //    Dim imgFileName As String = proposedFile.FileName.Remove(0, proposedFile.FileName.LastIndexOf("\") + 1)
        //    Dim imgContentType As String = proposedFile.ContentType


        //    ' Make sure that the posted file is actually an image
        //    If Not IsImageFile(imgFileName) Then
        //        Return -1
        //    End If
        //    ' Process the image into byte array
        //    Dim imgStream As Stream = proposedFile.InputStream
        //    Dim imgLen As Integer = proposedFile.ContentLength
        //    Dim imgBinaryData(imgLen) As Byte
        //    Dim n As Integer = imgStream.Read(imgBinaryData, 0, imgLen)

        //    ' Add the image to the database
        //    Dim conPortal As New SqlConnection(GlobalSetting.ConnectionString)
        //    Dim cmdAdd As New SqlCommand("Mouldnet_ImagesAddModuleImage", conPortal)
        //    cmdAdd.CommandType = CommandType.StoredProcedure

        //    cmdAdd.Parameters.Add(New SqlParameter("@RETURN_VALUE", SqlDbType.Int)).Direction = ParameterDirection.ReturnValue
        //    cmdAdd.Parameters.Add(New SqlParameter("@portalID", GlobalSetting.PortalID))
        //    cmdAdd.Parameters.Add(New SqlParameter("@modulePageID", modulePageID))
        //    cmdAdd.Parameters.Add(New SqlParameter("@imageName", imgFileName))
        //    cmdAdd.Parameters.Add(New SqlParameter("@contentType", imgContentType))
        //    cmdAdd.Parameters.Add(New SqlParameter("@imageData", SqlDbType.Image)).Value = imgBinaryData

        //    conPortal.Open()
        //    cmdAdd.ExecuteNonQuery()
        //    Dim result As Integer = Fix(cmdAdd.Parameters("@RETURN_VALUE").Value)
        //    conPortal.Close()

        //    Return result
        //End Function 'AddModuleImage


        // ''*********************************************************************
        // ''
        // '' UpdateModuleImage 方法
        // ''
        // '' Updates a section image in the database. 
        // ''
        // ''*********************************************************************
        //Public Shared Sub UpdateModuleImage(ByVal imageID As Integer, ByVal proposedFile As HttpPostedFile)
        //    ' Make sure that the posted file has content
        //    If proposedFile Is Nothing OrElse proposedFile.ContentLength = 0 Then
        //        Return
        //    End If
        //    Dim imgFileName As String = proposedFile.FileName.Remove(0, proposedFile.FileName.LastIndexOf("\") + 1)
        //    Dim imgContentType As String = proposedFile.ContentType


        //    ' Make sure that the posted file is actually an image
        //    If Not IsImageFile(imgFileName) Then
        //        Return
        //    End If
        //    ' Process the image into byte array
        //    Dim imgStream As Stream = proposedFile.InputStream
        //    Dim imgLen As Integer = proposedFile.ContentLength
        //    Dim imgBinaryData(imgLen) As Byte
        //    Dim n As Integer = imgStream.Read(imgBinaryData, 0, imgLen)

        //    ' Update the image in the database
        //    Dim conPortal As New SqlConnection(GlobalSetting.ConnectionString)
        //    Dim cmdAdd As New SqlCommand("Mouldnet_ImagesUpdateModuleImage", conPortal)
        //    cmdAdd.CommandType = CommandType.StoredProcedure

        //    cmdAdd.Parameters.Add(New SqlParameter("@RETURN_VALUE", SqlDbType.Int)).Direction = ParameterDirection.ReturnValue
        //    cmdAdd.Parameters.Add(New SqlParameter("@portalID", GlobalSetting.PortalID))
        //    cmdAdd.Parameters.Add(New SqlParameter("@imageID", imageID))
        //    cmdAdd.Parameters.Add(New SqlParameter("@imageName", imgFileName))
        //    cmdAdd.Parameters.Add(New SqlParameter("@contentType", imgContentType))
        //    cmdAdd.Parameters.Add(New SqlParameter("@imageData", SqlDbType.Image)).Value = imgBinaryData

        //    conPortal.Open()
        //    cmdAdd.ExecuteNonQuery()
        //    conPortal.Close()
        //End Sub 'UpdateModuleImage





        // ''*********************************************************************
        // ''
        // '' EditPortalImage 方法
        // ''
        // '' Updates a community image in the database. 
        // ''
        // ''*********************************************************************
        //'Public Shared Function EditPortalImage(ByVal originalFileName As String, ByVal imageType As PortalImageType, ByVal proposedFile As HttpPostedFile) As Integer
        //'    Dim imgFileName As String = proposedFile.FileName.Remove(0, proposedFile.FileName.LastIndexOf("\") + 1)
        //'    Dim imgContentType As String = proposedFile.ContentType

        //'    ' Process the image into byte array
        //'    Dim imgStream As Stream = proposedFile.InputStream
        //'    Dim imgLen As Integer = proposedFile.ContentLength
        //'    Dim imgBinaryData(imgLen) As Byte
        //'    Dim n As Integer = imgStream.Read(imgBinaryData, 0, imgLen)

        //'    ' Add the image to the database
        //'    Dim conPortal As New SqlConnection(GlobalSetting.ConnectionString)
        //'    Dim cmdEdit As New SqlCommand("Mouldnet_ImagesEditPortalImage", conPortal)
        //'    cmdEdit.CommandType = CommandType.StoredProcedure
        //'    cmdEdit.Parameters.Add(new SqlParameter("@RETURN_VALUE", SqlDbType.Int).Direction = ParameterDirection.ReturnValue
        //'    cmdEdit.Parameters.Add(new SqlParameter("@portalID", GlobalSetting.PortalID)
        //'    cmdEdit.Parameters.Add(new SqlParameter("@originalFileName", originalFileName)
        //'    cmdEdit.Parameters.Add(new SqlParameter("@fileName", imgFileName)
        //'    cmdEdit.Parameters.Add(new SqlParameter("@contentType", imgContentType)
        //'    cmdEdit.Parameters.Add(new SqlParameter("@imageType", imageType)
        //'    cmdEdit.Parameters.Add(new SqlParameter("@imageData", SqlDbType.Image).Value = imgBinaryData

        //'    conPortal.Open()
        //'    Dim result As Integer = cmdEdit.ExecuteNonQuery()
        //'    conPortal.Close()

        //'    Return result
        //'End Function 'EditPortalImage





        //'*********************************************************************
        //'
        //' BuildImagePath 方法
        //'
        //' Builds a URL to a module image given the image ID and image name. 
        //'
        //'*********************************************************************
        //Public Shared Function BuildImagePath(ByVal id As Integer, ByVal fileName As String) As String
        //    Dim extension As String = Path.GetExtension(fileName).ToLower()
        //    Return id.ToString() + extension
        //End Function 'BuildImagePath


        //'Public Shared Function CalculateImagePath(ByVal moduleID As Integer, ByVal id As Integer, ByVal fileName As String) As String
        //'    Dim imageFullName As String = id.ToString & Path.GetExtension(fileName).ToLower
        //'    Dim modulePath As String = ModuleUtility.GetModulePath(moduleID)
        //'    modulePath = modulePath.Remove(modulePath.LastIndexOf("/"), modulePath.Length - modulePath.LastIndexOf("/"))
        //'    Return String.Format("{0}/{1}", modulePath, imageFullName)
        //'End Function 'CalculateContentPath


        //'*********************************************************************
        //'
        //' BuildFullImagePath 方法
        //'
        //' Builds a URL to a section image that includes the section path. 
        //'
        //'*********************************************************************
        //'Public Shared Function BuildFullImagePath(ByVal moduleID As Integer, ByVal imageID As Integer, ByVal fileName As String) As String
        //'    Dim _path As String = ModuleUtility.GetModulePath(moduleID)
        //'    _path = _path.Remove(_path.LastIndexOf("/"), _path.Length - _path.LastIndexOf("/"))

        //'    Dim extension As String = Path.GetExtension(fileName).ToLower()
        //'    Return String.Format("{0}/{1}{2}", _path, imageID, extension)
        //'End Function 'BuildFullImagePath





        public enum ImageSubject
        {

            None = -1,
            Part = 0,
            Category = 1,
            Manufacturer = 2

        }
        //CommunityImageType


        public enum GlobalImageType
        {
            Global = 0,
            Logo = 1,
            Topic = 2
        }
        //CommunityImageType

        public enum TradeImageType
        {
            Model = 3,
            Drawing = 2,
            Sample = 1
        }
        //CommunityImageType

    }
    //ImageUtility 
}
